package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.handler.WorkException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.app.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private PaymentTypeService paymentTypeService;



    /**
     * 分页查询对应用户租约表
     * @param phone
     * @return
     */
    @Override
    public List<AgreementItemVo> getLeaseAgreementList(String phone) {
        return leaseAgreementMapper.getLeaseAgreementList(phone);
    }

    /**
     * 根据id获取租约信息，用于数据回显
     * @param id
     * @return
     */
    @Override
    public AgreementDetailVo getLeaseInfoById(Long id) {
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        //获取租约基本信息
        LeaseAgreement leaseAgreement = this.getById(id);
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        //查询公寓信息
        ApartmentDetailVo apartmentInfo = apartmentInfoService.getApartmentDetailVoById(leaseAgreement.getApartmentId());
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        //获取公寓图片列表
        List<GraphInfo> apartmentGraphInfo = graphInfoService.getApartmentGraphInfo(apartmentInfo.getId());
        List<GraphVo> graphInfos = new ArrayList<>();
        apartmentGraphInfo.forEach(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphInfos.add(graphVo);
        });
        agreementDetailVo.setApartmentGraphVoList(graphInfos);


        //查房间号
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        //房间图片列表
        List<GraphVo> roomGraphInfo = graphInfoService.getRoomGraphInfoById(roomInfo.getId());
        agreementDetailVo.setRoomGraphVoList(roomGraphInfo);
        //租期
        LeaseTerm leaseTerm = leaseTermService.getOne(new LambdaQueryWrapper<LeaseTerm>().eq(LeaseTerm::getId, leaseAgreement.getLeaseTermId()));
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());
        //支付方式
        PaymentType paymentType = paymentTypeService.getOne(new LambdaQueryWrapper<PaymentType>()
                .eq(PaymentType::getId, leaseAgreement.getPaymentTypeId()));
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        return agreementDetailVo;
    }
}




